{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 第十四章 智能推荐系统 - 协同过滤算法"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 14.2 相似度计算三种常见方法"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "14.2.1 欧式距离"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>用户1</th>\n",
       "      <th>用户2</th>\n",
       "      <th>用户3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>物品A</th>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>物品B</th>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>物品C</th>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     用户1  用户2  用户3\n",
       "物品A    5    1    5\n",
       "物品B    4    2    2\n",
       "物品C    4    2    1"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "df = pd.DataFrame([[5, 1, 5], [4, 2, 2], [4, 2, 1]], columns=['用户1', '用户2', '用户3'], index=['物品A', '物品B', '物品C'])\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.3166247903554"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "dist = np.linalg.norm(df.iloc[0] - df.iloc[1])\n",
    "dist"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 14.2.2 余弦内置函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>用户1</th>\n",
       "      <th>用户2</th>\n",
       "      <th>用户3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>物品A</th>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>物品B</th>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>物品C</th>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     用户1  用户2  用户3\n",
       "物品A    5    1    5\n",
       "物品B    4    2    2\n",
       "物品C    4    2    1"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "df = pd.DataFrame([[5, 1, 5], [4, 2, 2], [4, 2, 1]], columns=['用户1', '用户2', '用户3'], index=['物品A', '物品B', '物品C'])\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>物品A</th>\n",
       "      <th>物品B</th>\n",
       "      <th>物品C</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>物品A</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.914659</td>\n",
       "      <td>0.825029</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>物品B</th>\n",
       "      <td>0.914659</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.979958</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>物品C</th>\n",
       "      <td>0.825029</td>\n",
       "      <td>0.979958</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          物品A       物品B       物品C\n",
       "物品A  1.000000  0.914659  0.825029\n",
       "物品B  0.914659  1.000000  0.979958\n",
       "物品C  0.825029  0.979958  1.000000"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.metrics.pairwise import cosine_similarity\n",
    "user_similarity = cosine_similarity(df)\n",
    "pd.DataFrame(user_similarity, columns=['物品A', '物品B', '物品C'], index=['物品A', '物品B', '物品C'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 14.2.3 皮尔逊相关系数简单版"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "相关系数r值为-0.9938837346736188，显著性水平P值为0.0005736731093322215\n"
     ]
    }
   ],
   "source": [
    "from scipy.stats import pearsonr\n",
    "X = [1, 3, 5, 7, 9]\n",
    "Y = [9, 8, 6, 4, 2]\n",
    "corr = pearsonr(X, Y)\n",
    "print('相关系数r值为' + str(corr[0]) + '，显著性水平P值为' + str(corr[1]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 皮尔逊相关系数小案例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "df = pd.DataFrame([[5, 4, 4], [1, 2, 2], [5, 2, 1]], columns=['物品A', '物品B', '物品C'], index=['用户1', '用户2', '用户3'])  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>物品A</th>\n",
       "      <th>物品B</th>\n",
       "      <th>物品C</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>用户1</th>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>用户2</th>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>用户3</th>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     物品A  物品B  物品C\n",
       "用户1    5    4    4\n",
       "用户2    1    2    2\n",
       "用户3    5    2    1"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "物品A    1.000000\n",
       "物品B    0.500000\n",
       "物品C    0.188982\n",
       "dtype: float64"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 物品A与其他物品的皮尔逊相关系数\n",
    "A = df['物品A']\n",
    "corr_A = df.corrwith(A)\n",
    "corr_A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>物品A</th>\n",
       "      <th>物品B</th>\n",
       "      <th>物品C</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>物品A</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.500000</td>\n",
       "      <td>0.188982</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>物品B</th>\n",
       "      <td>0.500000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.944911</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>物品C</th>\n",
       "      <td>0.188982</td>\n",
       "      <td>0.944911</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          物品A       物品B       物品C\n",
       "物品A  1.000000  0.500000  0.188982\n",
       "物品B  0.500000  1.000000  0.944911\n",
       "物品C  0.188982  0.944911  1.000000"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 皮尔逊系数表，获取各物品相关性\n",
    "df.corr()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 14.3 案例实战 - 电影智能推荐系统"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.读取数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>电影编号</th>\n",
       "      <th>名称</th>\n",
       "      <th>类别</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>玩具总动员（1995）</td>\n",
       "      <td>冒险|动画|儿童|喜剧|幻想</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>勇敢者的游戏（1995）</td>\n",
       "      <td>冒险|儿童|幻想</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>斗气老顽童2（1995）</td>\n",
       "      <td>喜剧|爱情</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>待到梦醒时分（1995）</td>\n",
       "      <td>喜剧|剧情|爱情</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>新娘之父2（1995）</td>\n",
       "      <td>喜剧</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   电影编号            名称              类别\n",
       "0     1   玩具总动员（1995）  冒险|动画|儿童|喜剧|幻想\n",
       "1     2  勇敢者的游戏（1995）        冒险|儿童|幻想\n",
       "2     3  斗气老顽童2（1995）           喜剧|爱情\n",
       "3     4  待到梦醒时分（1995）        喜剧|剧情|爱情\n",
       "4     5   新娘之父2（1995）              喜剧"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd \n",
    "movies = pd.read_excel('电影.xlsx')\n",
    "movies.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>用户编号</th>\n",
       "      <th>电影编号</th>\n",
       "      <th>评分</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>47</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>50</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   用户编号  电影编号   评分\n",
       "0     1     1  4.0\n",
       "1     1     3  4.0\n",
       "2     1     6  4.0\n",
       "3     1    47  5.0\n",
       "4     1    50  5.0"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "score = pd.read_excel('评分.xlsx')\n",
    "score.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>电影编号</th>\n",
       "      <th>名称</th>\n",
       "      <th>类别</th>\n",
       "      <th>用户编号</th>\n",
       "      <th>评分</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>玩具总动员（1995）</td>\n",
       "      <td>冒险|动画|儿童|喜剧|幻想</td>\n",
       "      <td>1</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>玩具总动员（1995）</td>\n",
       "      <td>冒险|动画|儿童|喜剧|幻想</td>\n",
       "      <td>5</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>玩具总动员（1995）</td>\n",
       "      <td>冒险|动画|儿童|喜剧|幻想</td>\n",
       "      <td>7</td>\n",
       "      <td>4.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>玩具总动员（1995）</td>\n",
       "      <td>冒险|动画|儿童|喜剧|幻想</td>\n",
       "      <td>15</td>\n",
       "      <td>2.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>玩具总动员（1995）</td>\n",
       "      <td>冒险|动画|儿童|喜剧|幻想</td>\n",
       "      <td>17</td>\n",
       "      <td>4.5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   电影编号           名称              类别  用户编号   评分\n",
       "0     1  玩具总动员（1995）  冒险|动画|儿童|喜剧|幻想     1  4.0\n",
       "1     1  玩具总动员（1995）  冒险|动画|儿童|喜剧|幻想     5  4.0\n",
       "2     1  玩具总动员（1995）  冒险|动画|儿童|喜剧|幻想     7  4.5\n",
       "3     1  玩具总动员（1995）  冒险|动画|儿童|喜剧|幻想    15  2.5\n",
       "4     1  玩具总动员（1995）  冒险|动画|儿童|喜剧|幻想    17  4.5"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.merge(movies, score, on='电影编号')\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_excel('电影推荐系统.xlsx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4.0    26794\n",
       "3.0    20017\n",
       "5.0    13180\n",
       "3.5    13129\n",
       "4.5     8544\n",
       "2.0     7545\n",
       "2.5     5544\n",
       "1.0     2808\n",
       "1.5     1791\n",
       "0.5     1369\n",
       "Name: 评分, dtype: int64"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['评分'].value_counts()  # 查看各个评分的出现的次数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.axes._subplots.AxesSubplot at 0x24257d55588>"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "df['评分'].hist(bins=20)  # hist()函数绘制直方图，竖轴为各评分出现的次数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2.数据分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>评分</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>名称</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>假小子（1997）</th>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>福尔摩斯和华生医生历险记：讹诈之王（1980）</th>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>机器人（2016）</th>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>奥斯卡（1967）</th>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>人类状况III（1961）</th>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                          评分\n",
       "名称                          \n",
       "假小子（1997）                5.0\n",
       "福尔摩斯和华生医生历险记：讹诈之王（1980）  5.0\n",
       "机器人（2016）                5.0\n",
       "奥斯卡（1967）                5.0\n",
       "人类状况III（1961）            5.0"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ratings = pd.DataFrame(df.groupby('名称')['评分'].mean())\n",
    "ratings.sort_values('评分', ascending=False).head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>评分</th>\n",
       "      <th>评分次数</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>名称</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>阿甘正传（1994）</th>\n",
       "      <td>4.164134</td>\n",
       "      <td>329</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>肖申克的救赎（1994）</th>\n",
       "      <td>4.429022</td>\n",
       "      <td>317</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>低俗小说（1994）</th>\n",
       "      <td>4.197068</td>\n",
       "      <td>307</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>沉默的羔羊（1991）</th>\n",
       "      <td>4.161290</td>\n",
       "      <td>279</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>黑客帝国（1999）</th>\n",
       "      <td>4.192446</td>\n",
       "      <td>278</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                    评分  评分次数\n",
       "名称                          \n",
       "阿甘正传（1994）    4.164134   329\n",
       "肖申克的救赎（1994）  4.429022   317\n",
       "低俗小说（1994）    4.197068   307\n",
       "沉默的羔羊（1991）   4.161290   279\n",
       "黑客帝国（1999）    4.192446   278"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ratings['评分次数'] = df.groupby('名称')['评分'].count()\n",
    "ratings.sort_values('评分次数', ascending=False).head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.数据处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>名称</th>\n",
       "      <th>007之黄金眼（1995）</th>\n",
       "      <th>100个女孩（2000）</th>\n",
       "      <th>100条街道（2016）</th>\n",
       "      <th>101忠狗续集:伦敦大冒险（2003）</th>\n",
       "      <th>101忠狗（1961）</th>\n",
       "      <th>101雷克雅未克（2000）</th>\n",
       "      <th>102只斑点狗（2000）</th>\n",
       "      <th>10件或更少（2006）</th>\n",
       "      <th>10（1979）</th>\n",
       "      <th>11:14（2003）</th>\n",
       "      <th>...</th>\n",
       "      <th>龙珠：神秘冒险（1988）</th>\n",
       "      <th>龙珠：血红宝石的诅咒（1986）</th>\n",
       "      <th>龙珠：魔鬼城堡中的睡公主（1987）</th>\n",
       "      <th>龙种子（1944）</th>\n",
       "      <th>龙纹身的女孩（2011）</th>\n",
       "      <th>龙舌兰日出（1988）</th>\n",
       "      <th>龙虾（2015）</th>\n",
       "      <th>龙：夜之怒的礼物（2011）</th>\n",
       "      <th>龙：李小龙的故事（1993）</th>\n",
       "      <th>龟日记（1985）</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>用户编号</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>606</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>607</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>608</th>\n",
       "      <td>4.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>3.5</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>609</th>\n",
       "      <td>4.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>610</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4.5</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 9687 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "名称    007之黄金眼（1995）  100个女孩（2000）  100条街道（2016）  101忠狗续集:伦敦大冒险（2003）  \\\n",
       "用户编号                                                                   \n",
       "606             NaN           NaN           NaN                  NaN   \n",
       "607             NaN           NaN           NaN                  NaN   \n",
       "608             4.0           NaN           NaN                  NaN   \n",
       "609             4.0           NaN           NaN                  NaN   \n",
       "610             NaN           NaN           NaN                  NaN   \n",
       "\n",
       "名称    101忠狗（1961）  101雷克雅未克（2000）  102只斑点狗（2000）  10件或更少（2006）  10（1979）  \\\n",
       "用户编号                                                                       \n",
       "606           NaN             NaN            NaN           NaN       NaN   \n",
       "607           NaN             NaN            NaN           NaN       NaN   \n",
       "608           NaN             NaN            NaN           3.5       NaN   \n",
       "609           NaN             NaN            NaN           NaN       NaN   \n",
       "610           NaN             NaN            NaN           NaN       NaN   \n",
       "\n",
       "名称    11:14（2003）  ...  龙珠：神秘冒险（1988）  龙珠：血红宝石的诅咒（1986）  龙珠：魔鬼城堡中的睡公主（1987）  \\\n",
       "用户编号               ...                                                        \n",
       "606           NaN  ...            NaN               NaN                 NaN   \n",
       "607           NaN  ...            NaN               NaN                 NaN   \n",
       "608           NaN  ...            NaN               NaN                 NaN   \n",
       "609           NaN  ...            NaN               NaN                 NaN   \n",
       "610           NaN  ...            NaN               NaN                 NaN   \n",
       "\n",
       "名称    龙种子（1944）  龙纹身的女孩（2011）  龙舌兰日出（1988）  龙虾（2015）  龙：夜之怒的礼物（2011）  \\\n",
       "用户编号                                                                   \n",
       "606         NaN           NaN          NaN       NaN             NaN   \n",
       "607         NaN           NaN          NaN       NaN             NaN   \n",
       "608         NaN           NaN          NaN       NaN             NaN   \n",
       "609         NaN           NaN          NaN       NaN             NaN   \n",
       "610         NaN           4.0          NaN       4.5             NaN   \n",
       "\n",
       "名称    龙：李小龙的故事（1993）  龟日记（1985）  \n",
       "用户编号                             \n",
       "606              NaN        NaN  \n",
       "607              NaN        NaN  \n",
       "608              NaN        NaN  \n",
       "609              NaN        NaN  \n",
       "610              NaN        NaN  \n",
       "\n",
       "[5 rows x 9687 columns]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_movie = df.pivot_table(index='用户编号', columns='名称', values='评分')\n",
    "user_movie.tail()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>名称</th>\n",
       "      <th>007之黄金眼（1995）</th>\n",
       "      <th>100个女孩（2000）</th>\n",
       "      <th>100条街道（2016）</th>\n",
       "      <th>101忠狗续集:伦敦大冒险（2003）</th>\n",
       "      <th>101忠狗（1961）</th>\n",
       "      <th>101雷克雅未克（2000）</th>\n",
       "      <th>102只斑点狗（2000）</th>\n",
       "      <th>10件或更少（2006）</th>\n",
       "      <th>10（1979）</th>\n",
       "      <th>11:14（2003）</th>\n",
       "      <th>...</th>\n",
       "      <th>龙珠：神秘冒险（1988）</th>\n",
       "      <th>龙珠：血红宝石的诅咒（1986）</th>\n",
       "      <th>龙珠：魔鬼城堡中的睡公主（1987）</th>\n",
       "      <th>龙种子（1944）</th>\n",
       "      <th>龙纹身的女孩（2011）</th>\n",
       "      <th>龙舌兰日出（1988）</th>\n",
       "      <th>龙虾（2015）</th>\n",
       "      <th>龙：夜之怒的礼物（2011）</th>\n",
       "      <th>龙：李小龙的故事（1993）</th>\n",
       "      <th>龟日记（1985）</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>132.000000</td>\n",
       "      <td>4.00</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>44.000000</td>\n",
       "      <td>1.0</td>\n",
       "      <td>9.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>4.000000</td>\n",
       "      <td>4.00</td>\n",
       "      <td>...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>1.0</td>\n",
       "      <td>42.000000</td>\n",
       "      <td>13.000000</td>\n",
       "      <td>7.000000</td>\n",
       "      <td>1.0</td>\n",
       "      <td>8.00000</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>3.496212</td>\n",
       "      <td>3.25</td>\n",
       "      <td>2.5</td>\n",
       "      <td>2.5</td>\n",
       "      <td>3.431818</td>\n",
       "      <td>3.5</td>\n",
       "      <td>2.777778</td>\n",
       "      <td>2.666667</td>\n",
       "      <td>3.375000</td>\n",
       "      <td>3.75</td>\n",
       "      <td>...</td>\n",
       "      <td>3.5</td>\n",
       "      <td>3.5</td>\n",
       "      <td>3.250000</td>\n",
       "      <td>3.5</td>\n",
       "      <td>3.488095</td>\n",
       "      <td>3.038462</td>\n",
       "      <td>4.000000</td>\n",
       "      <td>5.0</td>\n",
       "      <td>2.81250</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>0.859381</td>\n",
       "      <td>0.50</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.751672</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.833333</td>\n",
       "      <td>1.040833</td>\n",
       "      <td>1.030776</td>\n",
       "      <td>0.50</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.353553</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.327422</td>\n",
       "      <td>0.431158</td>\n",
       "      <td>0.707107</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.03294</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>0.500000</td>\n",
       "      <td>2.50</td>\n",
       "      <td>2.5</td>\n",
       "      <td>2.5</td>\n",
       "      <td>1.500000</td>\n",
       "      <td>3.5</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>1.500000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>3.00</td>\n",
       "      <td>...</td>\n",
       "      <td>3.5</td>\n",
       "      <td>3.5</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>3.5</td>\n",
       "      <td>0.500000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>5.0</td>\n",
       "      <td>0.50000</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>3.000000</td>\n",
       "      <td>3.25</td>\n",
       "      <td>2.5</td>\n",
       "      <td>2.5</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>3.5</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>2.250000</td>\n",
       "      <td>3.125000</td>\n",
       "      <td>3.75</td>\n",
       "      <td>...</td>\n",
       "      <td>3.5</td>\n",
       "      <td>3.5</td>\n",
       "      <td>3.125000</td>\n",
       "      <td>3.5</td>\n",
       "      <td>2.625000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>3.500000</td>\n",
       "      <td>5.0</td>\n",
       "      <td>2.87500</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>3.500000</td>\n",
       "      <td>3.50</td>\n",
       "      <td>2.5</td>\n",
       "      <td>2.5</td>\n",
       "      <td>3.500000</td>\n",
       "      <td>3.5</td>\n",
       "      <td>2.500000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>3.500000</td>\n",
       "      <td>4.00</td>\n",
       "      <td>...</td>\n",
       "      <td>3.5</td>\n",
       "      <td>3.5</td>\n",
       "      <td>3.250000</td>\n",
       "      <td>3.5</td>\n",
       "      <td>4.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>4.000000</td>\n",
       "      <td>5.0</td>\n",
       "      <td>3.00000</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>4.000000</td>\n",
       "      <td>3.50</td>\n",
       "      <td>2.5</td>\n",
       "      <td>2.5</td>\n",
       "      <td>4.000000</td>\n",
       "      <td>3.5</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>3.250000</td>\n",
       "      <td>3.750000</td>\n",
       "      <td>4.00</td>\n",
       "      <td>...</td>\n",
       "      <td>3.5</td>\n",
       "      <td>3.5</td>\n",
       "      <td>3.375000</td>\n",
       "      <td>3.5</td>\n",
       "      <td>4.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>4.500000</td>\n",
       "      <td>5.0</td>\n",
       "      <td>3.12500</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>5.000000</td>\n",
       "      <td>3.50</td>\n",
       "      <td>2.5</td>\n",
       "      <td>2.5</td>\n",
       "      <td>5.000000</td>\n",
       "      <td>3.5</td>\n",
       "      <td>4.500000</td>\n",
       "      <td>3.500000</td>\n",
       "      <td>4.500000</td>\n",
       "      <td>4.00</td>\n",
       "      <td>...</td>\n",
       "      <td>3.5</td>\n",
       "      <td>3.5</td>\n",
       "      <td>3.500000</td>\n",
       "      <td>3.5</td>\n",
       "      <td>5.000000</td>\n",
       "      <td>4.000000</td>\n",
       "      <td>5.000000</td>\n",
       "      <td>5.0</td>\n",
       "      <td>4.00000</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>8 rows × 9687 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "名称     007之黄金眼（1995）  100个女孩（2000）  100条街道（2016）  101忠狗续集:伦敦大冒险（2003）  \\\n",
       "count     132.000000          4.00           1.0                  1.0   \n",
       "mean        3.496212          3.25           2.5                  2.5   \n",
       "std         0.859381          0.50           NaN                  NaN   \n",
       "min         0.500000          2.50           2.5                  2.5   \n",
       "25%         3.000000          3.25           2.5                  2.5   \n",
       "50%         3.500000          3.50           2.5                  2.5   \n",
       "75%         4.000000          3.50           2.5                  2.5   \n",
       "max         5.000000          3.50           2.5                  2.5   \n",
       "\n",
       "名称     101忠狗（1961）  101雷克雅未克（2000）  102只斑点狗（2000）  10件或更少（2006）  10（1979）  \\\n",
       "count    44.000000             1.0       9.000000      3.000000  4.000000   \n",
       "mean      3.431818             3.5       2.777778      2.666667  3.375000   \n",
       "std       0.751672             NaN       0.833333      1.040833  1.030776   \n",
       "min       1.500000             3.5       2.000000      1.500000  2.000000   \n",
       "25%       3.000000             3.5       2.000000      2.250000  3.125000   \n",
       "50%       3.500000             3.5       2.500000      3.000000  3.500000   \n",
       "75%       4.000000             3.5       3.000000      3.250000  3.750000   \n",
       "max       5.000000             3.5       4.500000      3.500000  4.500000   \n",
       "\n",
       "名称     11:14（2003）  ...  龙珠：神秘冒险（1988）  龙珠：血红宝石的诅咒（1986）  龙珠：魔鬼城堡中的睡公主（1987）  \\\n",
       "count         4.00  ...            1.0               1.0            2.000000   \n",
       "mean          3.75  ...            3.5               3.5            3.250000   \n",
       "std           0.50  ...            NaN               NaN            0.353553   \n",
       "min           3.00  ...            3.5               3.5            3.000000   \n",
       "25%           3.75  ...            3.5               3.5            3.125000   \n",
       "50%           4.00  ...            3.5               3.5            3.250000   \n",
       "75%           4.00  ...            3.5               3.5            3.375000   \n",
       "max           4.00  ...            3.5               3.5            3.500000   \n",
       "\n",
       "名称     龙种子（1944）  龙纹身的女孩（2011）  龙舌兰日出（1988）  龙虾（2015）  龙：夜之怒的礼物（2011）  \\\n",
       "count        1.0     42.000000    13.000000  7.000000             1.0   \n",
       "mean         3.5      3.488095     3.038462  4.000000             5.0   \n",
       "std          NaN      1.327422     0.431158  0.707107             NaN   \n",
       "min          3.5      0.500000     2.000000  3.000000             5.0   \n",
       "25%          3.5      2.625000     3.000000  3.500000             5.0   \n",
       "50%          3.5      4.000000     3.000000  4.000000             5.0   \n",
       "75%          3.5      4.000000     3.000000  4.500000             5.0   \n",
       "max          3.5      5.000000     4.000000  5.000000             5.0   \n",
       "\n",
       "名称     龙：李小龙的故事（1993）  龟日记（1985）  \n",
       "count         8.00000        2.0  \n",
       "mean          2.81250        4.0  \n",
       "std           1.03294        0.0  \n",
       "min           0.50000        4.0  \n",
       "25%           2.87500        4.0  \n",
       "50%           3.00000        4.0  \n",
       "75%           3.12500        4.0  \n",
       "max           4.00000        4.0  \n",
       "\n",
       "[8 rows x 9687 columns]"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_movie.describe()  # 因为数据量较大，这个耗时可能会有1分钟左右"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4.智能推荐"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>阿甘正传（1994）</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>用户编号</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      阿甘正传（1994）\n",
       "用户编号            \n",
       "1            4.0\n",
       "2            NaN\n",
       "3            NaN\n",
       "4            NaN\n",
       "5            NaN"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "FG = user_movie['阿甘正传（1994）']  # FG是Forrest Gump（），阿甘英文名称的缩写\n",
    "pd.DataFrame(FG).head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\ProgramData\\Anaconda3\\lib\\site-packages\\numpy\\lib\\function_base.py:2522: RuntimeWarning: Degrees of freedom <= 0 for slice\n",
      "  c = cov(x, y, rowvar)\n",
      "C:\\ProgramData\\Anaconda3\\lib\\site-packages\\numpy\\lib\\function_base.py:2451: RuntimeWarning: divide by zero encountered in true_divide\n",
      "  c *= np.true_divide(1, fact)\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>相关系数</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>名称</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>007之黄金眼（1995）</th>\n",
       "      <td>0.217441</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>100个女孩（2000）</th>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>100条街道（2016）</th>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>101忠狗续集:伦敦大冒险（2003）</th>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>101忠狗（1961）</th>\n",
       "      <td>0.141023</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                         相关系数\n",
       "名称                           \n",
       "007之黄金眼（1995）        0.217441\n",
       "100个女孩（2000）              NaN\n",
       "100条街道（2016）              NaN\n",
       "101忠狗续集:伦敦大冒险（2003）       NaN\n",
       "101忠狗（1961）          0.141023"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# axis默认为0，计算user_movie各列与FG的相关系数\n",
    "corr_FG = user_movie.corrwith(FG)\n",
    "similarity = pd.DataFrame(corr_FG, columns=['相关系数'])\n",
    "similarity.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>相关系数</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>名称</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>007之黄金眼（1995）</th>\n",
       "      <td>0.217441</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>101忠狗（1961）</th>\n",
       "      <td>0.141023</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102只斑点狗（2000）</th>\n",
       "      <td>-0.857589</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10件或更少（2006）</th>\n",
       "      <td>-1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11:14（2003）</th>\n",
       "      <td>0.500000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   相关系数\n",
       "名称                     \n",
       "007之黄金眼（1995）  0.217441\n",
       "101忠狗（1961）    0.141023\n",
       "102只斑点狗（2000） -0.857589\n",
       "10件或更少（2006）  -1.000000\n",
       "11:14（2003）    0.500000"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "similarity.dropna(inplace=True)  # 或写成similarity=similarity.dropna()\n",
    "similarity.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>相关系数</th>\n",
       "      <th>评分次数</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>名称</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>007之黄金眼（1995）</th>\n",
       "      <td>0.217441</td>\n",
       "      <td>132</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>101忠狗（1961）</th>\n",
       "      <td>0.141023</td>\n",
       "      <td>44</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102只斑点狗（2000）</th>\n",
       "      <td>-0.857589</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10件或更少（2006）</th>\n",
       "      <td>-1.000000</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11:14（2003）</th>\n",
       "      <td>0.500000</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   相关系数  评分次数\n",
       "名称                           \n",
       "007之黄金眼（1995）  0.217441   132\n",
       "101忠狗（1961）    0.141023    44\n",
       "102只斑点狗（2000） -0.857589     9\n",
       "10件或更少（2006）  -1.000000     3\n",
       "11:14（2003）    0.500000     4"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "similarity_new = pd.merge(similarity, ratings['评分次数'], left_index=True, right_index=True)\n",
    "similarity_new.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>相关系数</th>\n",
       "      <th>评分次数</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>名称</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>007之黄金眼（1995）</th>\n",
       "      <td>0.217441</td>\n",
       "      <td>132</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>101忠狗（1961）</th>\n",
       "      <td>0.141023</td>\n",
       "      <td>44</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102只斑点狗（2000）</th>\n",
       "      <td>-0.857589</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10件或更少（2006）</th>\n",
       "      <td>-1.000000</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11:14（2003）</th>\n",
       "      <td>0.500000</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   相关系数  评分次数\n",
       "名称                           \n",
       "007之黄金眼（1995）  0.217441   132\n",
       "101忠狗（1961）    0.141023    44\n",
       "102只斑点狗（2000） -0.857589     9\n",
       "10件或更少（2006）  -1.000000     3\n",
       "11:14（2003）    0.500000     4"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 第二种合并方式\n",
    "similarity_new = similarity.join(ratings['评分次数'])\n",
    "similarity_new.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>相关系数</th>\n",
       "      <th>评分次数</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>名称</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>阿甘正传（1994）</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>329</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>抓狂双宝（1996）</th>\n",
       "      <td>0.723238</td>\n",
       "      <td>31</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>雷神：黑暗世界（2013）</th>\n",
       "      <td>0.715809</td>\n",
       "      <td>21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>致命吸引力（1987）</th>\n",
       "      <td>0.701856</td>\n",
       "      <td>36</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>X战警：未来的日子（2014）</th>\n",
       "      <td>0.682284</td>\n",
       "      <td>30</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     相关系数  评分次数\n",
       "名称                             \n",
       "阿甘正传（1994）       1.000000   329\n",
       "抓狂双宝（1996）       0.723238    31\n",
       "雷神：黑暗世界（2013）    0.715809    21\n",
       "致命吸引力（1987）      0.701856    36\n",
       "X战警：未来的日子（2014）  0.682284    30"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "similarity_new[similarity_new['评分次数'] > 20].sort_values(by='相关系数', ascending=False).head()  # 选取阈值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 补充知识点：groupby()函数的使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "data = pd.DataFrame([['战狼2', '丁一', 6, 8], ['攀登者', '王二', 8, 6], ['攀登者', '张三', 10, 8], ['卧虎藏龙', '李四', 8, 8], ['卧虎藏龙', '赵五', 8, 10]], columns=['电影名称', '影评师', '观前评分', '观后评分'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>电影名称</th>\n",
       "      <th>影评师</th>\n",
       "      <th>观前评分</th>\n",
       "      <th>观后评分</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>战狼2</td>\n",
       "      <td>丁一</td>\n",
       "      <td>6</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>攀登者</td>\n",
       "      <td>王二</td>\n",
       "      <td>8</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>攀登者</td>\n",
       "      <td>张三</td>\n",
       "      <td>10</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>卧虎藏龙</td>\n",
       "      <td>李四</td>\n",
       "      <td>8</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>卧虎藏龙</td>\n",
       "      <td>赵五</td>\n",
       "      <td>8</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   电影名称 影评师  观前评分  观后评分\n",
       "0   战狼2  丁一     6     8\n",
       "1   攀登者  王二     8     6\n",
       "2   攀登者  张三    10     8\n",
       "3  卧虎藏龙  李四     8     8\n",
       "4  卧虎藏龙  赵五     8    10"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>观后评分</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>电影名称</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>卧虎藏龙</th>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>战狼2</th>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>攀登者</th>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      观后评分\n",
       "电影名称      \n",
       "卧虎藏龙     9\n",
       "战狼2      8\n",
       "攀登者      7"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "means = data.groupby('电影名称')[['观后评分']].mean()\n",
    "means"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>观前评分</th>\n",
       "      <th>观后评分</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>电影名称</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>卧虎藏龙</th>\n",
       "      <td>8</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>战狼2</th>\n",
       "      <td>6</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>攀登者</th>\n",
       "      <td>9</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      观前评分  观后评分\n",
       "电影名称            \n",
       "卧虎藏龙     8     9\n",
       "战狼2      6     8\n",
       "攀登者      9     7"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "means = data.groupby('电影名称')[['观前评分', '观后评分']].mean()\n",
    "means"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>观后评分</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>电影名称</th>\n",
       "      <th>影评师</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">卧虎藏龙</th>\n",
       "      <th>李四</th>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>赵五</th>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>战狼2</th>\n",
       "      <th>丁一</th>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">攀登者</th>\n",
       "      <th>张三</th>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王二</th>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          观后评分\n",
       "电影名称 影评师      \n",
       "卧虎藏龙 李四      8\n",
       "     赵五     10\n",
       "战狼2  丁一      8\n",
       "攀登者  张三      8\n",
       "     王二      6"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "means = data.groupby(['电影名称', '影评师'])[['观后评分']].mean()\n",
    "means"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>观后评分</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>电影名称</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>卧虎藏龙</th>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>战狼2</th>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>攀登者</th>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      观后评分\n",
       "电影名称      \n",
       "卧虎藏龙     2\n",
       "战狼2      1\n",
       "攀登者      2"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "count = data.groupby('电影名称')[['观后评分']].count()\n",
    "count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>评分次数</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>电影名称</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>卧虎藏龙</th>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>战狼2</th>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>攀登者</th>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      评分次数\n",
       "电影名称      \n",
       "卧虎藏龙     2\n",
       "战狼2      1\n",
       "攀登者      2"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "count = count.rename(columns={'观后评分':'评分次数'})\n",
    "count"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
